﻿/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 23/04/2014
 * Time: 07:13 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
using System.Collections.Generic;
using Gestión_UTN.Entities;

namespace Gestión_UTN.daimpl
{
	/// <summary>
	/// Description of aprobadadaimpl.
	/// </summary>
	public class aprobadadaimpl:da.aprobadada
	{
		public aprobadadaimpl()
		{
		}
		
		public bool SaveAprobada(int alu_dni, int mat_id, int cur_id, DateTime fecha)
		{
			bool ingresada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "INSERT INTO apr_aprobadas(alu_dni, mat_id, cur_id, apr_fecha)" +
                    			"VALUES(@alu_dni, @mat_id, @cur_id, @apr_fecha)";
					
					OleDbCommand insertar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					object[] values = {alu_dni,mat_id,cur_id,fecha};
					
					insertar.Parameters.AddRange(values);
					
					insertar.ExecuteNonQuery();
					
					trans.Commit();
					
					ingresada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				ingresada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return ingresada;
		}
		
		public bool DeleteAprobada(int alu_dni, int mat_id, DateTime fecha)
		{
			bool eliminada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "DELETE FROM apr_aprobadas " +
						"WHERE alu_dni = @alu_dni AND mat_id = @mat_id AND apr_fecha = @fecha";
					
					OleDbCommand eliminar = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					trans = conex.BeginTransaction();
					
					object[] values = {alu_dni,mat_id,fecha};
					
					eliminar.Parameters.AddRange(values);
					
					eliminar.ExecuteNonQuery();
					
					trans.Commit();
					
					eliminada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				eliminada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return eliminada;
		}
		
		public bool ModifyAprobada(int alu_dni_anterior, int mat_id_anterior, DateTime fecha_anterior, int alu_dni, int mat_id, int cur_id, DateTime fecha)
		{
			bool modificada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "UPDATE apr_aprobadas SET alu_dni = @alu_dni, mat_id = @mat_id, cur_id = @cur_id, apr_fecha = @fecha " +
									"WHERE alu_dni = @alu_dni_anterior AND mat_id = @mat_id_anterior AND apr_fecha = @fecha_anterior";
					
					OleDbCommand modificar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					object[] values = {alu_dni,mat_id,cur_id,fecha,alu_dni_anterior,mat_id_anterior,fecha_anterior};
					
					modificar.Parameters.AddRange(values);
					
					modificar.ExecuteNonQuery();
					
					trans.Commit();
					
					modificada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				modificada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return modificada;
		}
		
		public System.Data.DataTable FindAllAprobadas()
		{
			DataTable tablaAprobadas = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				tablaAprobadas = new DataTable();
				
				if (null != conex) {
					
					string sql = "SELECT alu_dni, alu_nombre, alu_apellido, mat_nombre, " +
									"cur_anio, cur_cuatrimestre, apr_fecha" +
									" FROM apr_aprobadas apr, alu_alumnos alu, mat_materias mat, cur_cursadas cur" +
									" WHERE apr.alu_dni = alu.alu_dni AND apr.mat_id = mat.mat_id AND apr.cur_id = cur.cur_id";
					
					 OleDbDataAdapter adaptador = new OleDbDataAdapter(sql, conex);
					 
					 conex.Open();
					 
					 adaptador.Fill(tablaAprobadas);
					
					 conex.Close();
				}
				
			} catch (Exception) {
				
				tablaAprobadas = null;
				conex.Close();
			}
			
			return tablaAprobadas;
		}
		
		public System.Collections.Generic.List<Gestión_UTN.Entities.Aprobada> FindAprobadaByDni(int alu_dni)
		{
			List<Aprobada> aprobadas = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				aprobadas = new List<Aprobada>();
				
				if (null != conex) {
					
					string sql = "SELECT alu_dni, mat_id, cur_id, apr_fecha " +
									" FROM apr_aprobadas reg" +
									" WHERE alu_dni = @alu_dni";
					
					 OleDbCommand consulta = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					consulta.Parameters.AddWithValue("@alu_dni",alu_dni);
					
					OleDbDataReader lector = consulta.ExecuteReader();
					
					while (lector.Read()) 
					{
						Aprobada apr = new Aprobada(Convert.ToInt16(lector[0].ToString()),
						                                  Convert.ToInt16(lector[1].ToString()),
						                                  Convert.ToInt16(lector[2].ToString()),
						                                  Convert.ToDateTime(lector[4].ToString()));
						aprobadas.Add(apr);
						
						apr = null;
					}
					
					conex.Close();
				}
				
			} catch (Exception) {
				
				aprobadas = null;
				conex.Close();
			}
			
			return aprobadas;
		}
		
		public System.Collections.Generic.List<Gestión_UTN.Entities.Aprobada> FindAprobadaByMateria(int mat_id, int cur_id)
		{
			List<Aprobada> aprobadas = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				aprobadas = new List<Aprobada>();
				
				if (null != conex) {
					
					string sql = "SELECT alu_dni, mat_id, cur_id, apr_fecha " +
									" FROM apr_aprobadas apr" +
									" WHERE mat_id = @mat_id AND cur_id = @cur_id";
					
					 OleDbCommand consulta = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					consulta.Parameters.AddWithValue("@mat_id",mat_id);
					consulta.Parameters.AddWithValue("@cur_id",cur_id);
					
					OleDbDataReader lector = consulta.ExecuteReader();
					
					while (lector.Read()) 
					{
						Aprobada apr = new Aprobada(Convert.ToInt16(lector[0].ToString()),
						                                  Convert.ToInt16(lector[1].ToString()),
						                                  Convert.ToInt16(lector[2].ToString()),
						                                  Convert.ToDateTime(lector[4].ToString()));
						aprobadas.Add(apr);
						
						apr = null;
					}
					
					conex.Close();
				}
				
			} catch (Exception) {
				
				aprobadas = null;
				conex.Close();
			}
			
			return aprobadas;
		}
	}
}
